package goodbaby.dklt.customerview;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tgd.gbb.uikit.ui.widget.MyListView;
import com.tgd.gbb.uikit.ui.widget.asyncimageview.CircleImageView;

import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.adapter.CommonAdapter;
import goodbaby.dklt.adapter.ViewHolder;

/**
 * Created by Li.z on 2016/4/30.
 * 排行榜通用视图
 */
public class Top5ListView extends FrameLayout {
    private View contentView;
    private TextView tvTitle;
    private MyListView listView;

    private View llTitle;

    private List<Top5Data> datas;

    private String usernameColor = "#525252";
    private String numberColor = "#27b8f2";
    private String positionColor = "#e38a37";

    private int titleBackgroundRes = R.mipmap.health_bang_title;

    public Top5ListView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        initView(context, attrs);
    }

    public Top5ListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        contentView = View.inflate(context, R.layout.view_top5_list, this);
        tvTitle = (TextView) contentView.findViewById(R.id.tv_top_title);
        llTitle = contentView.findViewById(R.id.ll_title);
        listView = (MyListView) contentView.findViewById(R.id.mlv_top_listview);

        listView.setFocusable(false);

    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setUsernameColor(String color) {
        usernameColor = color;
    }

    public void setNumberColor(String color) {
        numberColor = color;
    }

    public void setPositionColor(String color) {
        positionColor = color;
    }

    public void setTitleBackground(int resId) {
        titleBackgroundRes = resId;
        llTitle.setBackgroundResource(titleBackgroundRes);
    }

    public void setDatas(DataAdapter adapter) {
        datas = adapter.getData();

        if (datas == null) {
            return;
        }

        listView.setAdapter(new CommonAdapter<Top5Data>(getContext(), R.layout.item_list_top5, datas) {
            @Override
            public void convert(ViewHolder helper, Top5Data item, int position) {
                TextView tvPosition = helper.getView(R.id.tv_position);
                tvPosition.setTextColor(Color.parseColor(positionColor));
                tvPosition.setText("0" + (position + 1));

                TextView tvUsername = helper.getView(R.id.tv_username);
                tvUsername.setTextColor(Color.parseColor(usernameColor));
                tvUsername.setText(item.username);

                TextView tvNumber = helper.getView(R.id.tv_number);
                tvNumber.setTextColor(Color.parseColor(numberColor));
                tvNumber.setText(item.data);

                CircleImageView imageView = helper.getView(R.id.civ_head_image);
                if (!TextUtils.isEmpty(item.image)) {
                    Picasso.with(mContext).load(item.image).into(imageView);
                }
            }

            @Override
            public int getCount() {
                if (datas.size() > 5) {
                    return 5;
                }
                return datas.size();
            }
        });
    }

    public interface DataAdapter {
        List<Top5Data> getData();
    }

    public static class Top5Data {
        public String image;
        public String username;
        public String data;

        public Top5Data(String image, String username, String data) {
            this.image = image;
            this.username = username;
            this.data = data;
        }
    }

}
